package com.konne.nightmare.FastPublicOpinion.ui;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.SizeUtils;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.common.GlideEngine;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.ImportTypePopup;
import com.konne.nightmare.FastPublicOpinion.ui.dialog.LocalUploadDialog;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.AddCoursewareAct;
import com.konne.nightmare.FastPublicOpinion.ui.information.activity.ImportDatabaseAct;
import com.konne.nightmare.FastPublicOpinion.utils.GlideUtils;
import com.konne.nightmare.FastPublicOpinion.widget.PicShowView;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.List;

/**
 * Create by ws.
 * on Data:2022/2/15  Time:下午 1:54.
 * description: ** .
 */
public class AddDataHelper {
    private List<LocalMedia> images;
    private  PicShowView picShow;
    private LinearLayout dataContent;
    private LayoutInflater inflater;
    private Activity activity;
    public AddDataHelper(Activity activity,PicShowView picShow, LinearLayout dataContent) {
        this.activity=activity;
        this.picShow = picShow;
        this.dataContent = dataContent;
        inflater=LayoutInflater.from(activity);
    }
    public void showLocalUpload(int type) {
        if (type == 0) {
            PictureSelector.create(activity)
                    .openGallery(PictureMimeType.ofAll())
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .isCamera(false)
                    .isWithVideoImage(true)
                    .isWeChatStyle(true)
                    .compress(true)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .maxVideoSelectNum(5)
                    .maxSelectNum(12)
                    .selectionData(images)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }else if(type==1){
            PictureSelector.create(activity)
                    .openCamera(PictureMimeType.ofAll())
                    .loadImageEngine(GlideEngine.createGlideEngine())
                    .isUseCustomCamera(true)
                    .isWithVideoImage(true)
                    .isWeChatStyle(true)
                    .compress(true)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .maxVideoSelectNum(5)
                    .maxSelectNum(12)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }else {

        }
    }
    /**
     * 添加已有的资料。里应加资料数据
     * */
    public void addDataItem(){
        dataContent.addView(getItemHasData(dataContent.getChildCount()));
    }
    public void removeDataItem(int index){
        dataContent.removeView(dataContent.getChildAt(index));
    }
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            images = PictureSelector.obtainMultipleResult(data);
            setPicShow();
        }
    }
    private void setPicShow() {
        if(picShow==null){
            return;
        }
        if (images == null || images.size() == 0) {
            picShow.setVisibility(View.GONE);
            return;
        }
        picShow.setVisibility(View.VISIBLE);
        picShow.removeAllViews();
        for (int i = 0; i < images.size(); i++) {
            LocalMedia media=images.get(i);
            View view=getItemPicData(media);
            picShow.addView(view);
            view.findViewById(R.id.iv_del).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    images.remove(media);
                    picShow.removeView(view);
                    picShow.requestLayout();
                    if (images == null || images.size() == 0) {
                        picShow.setVisibility(View.GONE);
                        return;
                    }
                }
            });
        }
    }
    private View getItemPicData(LocalMedia media) {
        View view = inflater.inflate(R.layout.item_show_pic, null);
        ImageView pic=view.findViewById(R.id.iv_pic);
        GlideUtils.loadImage(media.getRealPath(),pic);
        if(media.getMimeType().equals(PictureMimeType.ofMP4())|| media.getMimeType().equals(PictureMimeType.ofAVI())){
            view.findViewById(R.id.iv_vod).setVisibility(View.VISIBLE);
        }else {
            view.findViewById(R.id.iv_vod).setVisibility(View.GONE);
        }
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        return view;
    }
    private View getItemHasData(int index) {
        View view = inflater.inflate(R.layout.item_has_data, null);
        ImageView pic=view.findViewById(R.id.iv_pic);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, SizeUtils.dp2px(80));
        params.topMargin=SizeUtils.dp2px(10);
        view.setLayoutParams(params);
        view.findViewById(R.id.iv_remove).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                removeDataItem(index);
            }
        });
        view.findViewById(R.id.stv_change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImportTypePopup.show(activity, view, new ImportTypePopup.ImportTypeListener() {
                    @Override
                    public void importType(int type) {
                        showImportType(type);
                    }
                });
            }
        });
        return view;
    }
    private void showImportType(int type){
        switch (type){
            case 0:
                new LocalUploadDialog(activity).show();
                break;
            case 1://导入已有
                break;
            case 2:
                ImportDatabaseAct.start(2);
                break;
        }
    }
}
